package com.genfu.reform.controller;

import org.apache.struts2.dispatcher.SessionMap;

import com.genfu.reform.model.UserInfo;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

public class GenfuAuthenticationInterceptor implements Interceptor {
	private static final long serialVersionUID = 1L;
	private static final Logger LOG = LoggerFactory.getLogger(GenfuAuthenticationInterceptor.class);
	public static final String USER_SESSION_KEY = "genfuUserSessionKey";
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		LOG.debug("Authenticating chat user");

		SessionMap<?, ?> session = (SessionMap<?, ?>) ActionContext.getContext().get(ActionContext.SESSION);
		UserInfo user = (UserInfo) session.get(USER_SESSION_KEY);

		if (user == null) {
			return Action.LOGIN;
		}
		return invocation.invoke();
	}

}
